﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace GrayParrot.Web.Controls
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:AlphaLinks runat=server></{0}:AlphaLinks>")]
    public class AlphaLinks : Repeater
    {
        string[] letters = { "All", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K",
					         "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
					         "W", "X", "Y", "Z"};

        private string selectedLetter;
        //private int selectedIndex;

        public string Letter
        {
            get
            {
                string m_letter = string.Empty;

                if (ViewState["selectedLetter"] != null)
                {
                    m_letter = ViewState["selectedLetter"].ToString();
                }

                return m_letter;
            }
            set
            {
                ViewState["selectedLetter"] = value;
            }
        }

        public void Select(object sender, CommandEventArgs e)
        {
            selectedLetter = e.CommandArgument.ToString();
            ViewState["selectedLetter"] = e.CommandArgument.ToString();
        }

        public void DisableSelectedLink(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.Controls.Count > 0)
            {
                LinkButton lb = (LinkButton)e.Item.Controls[0];
                if (lb.Text == Letter)
                    lb.Enabled = false;
            }
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            this.DataSource = letters;
            this.DataBind();

            foreach (RepeaterItem ri in this.Items)
            {
                int index = ri.ItemIndex;
                string m_letter = ((string[])this.DataSource)[index];

                LinkButton lnk = new LinkButton();
                lnk.Text = string.Format("{0}", m_letter);
                lnk.CommandName = "Filter";
                lnk.CommandArgument = m_letter;
                lnk.Command += lnk_Command;

                ri.Controls.Add(lnk);
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            if (ViewState["selectedLetter"] == null)
            {
                selectedLetter = "All";
                ViewState["selectedLetter"] = "All";
            }
        }

        public override void RenderControl(HtmlTextWriter writer)
        {
            writer.RenderBeginTag(HtmlTextWriterTag.Table);
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);

            foreach (RepeaterItem ri in this.Items)
            {
                writer.RenderBeginTag(HtmlTextWriterTag.Td);

                LinkButton lnk = (LinkButton)ri.Controls[0];

                if (lnk.Text == Letter)
                    lnk.Enabled = false;

                lnk.RenderControl(writer);

                writer.RenderEndTag(); // Td
            }

            writer.RenderEndTag(); // Tr
            writer.RenderEndTag(); // table
        }

        protected override void OnItemDataBound(RepeaterItemEventArgs e)
        {
            DisableSelectedLink(this, e);
        }

        void lnk_Command(object sender, CommandEventArgs e)
        {
            Select(sender, e);
        }
    }
}
